package com.bt.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bt.annotation.RequiresPermissionsDesc;
import com.bt.pojo.DtsOrder;
import com.bt.pojo.DtsOrderGoods;
import com.bt.pojo.DtsUser;
import com.bt.service.DtsOrderGoodsService;
import com.bt.service.DtsOrderService;
import com.bt.service.DtsUserService;
import com.bt.util.ResponseUtil;
import com.bt.vo.ShipChannelVo;
import com.bt.vo.UserVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wbt
 * @version v1.0
 * @project shop_manage
 * @data 2022/9/24 21:01
 **/
@RestController
@RequestMapping("/admin/order")
@CrossOrigin
public class AdminOrderController {
    @Autowired
    private DtsOrderService dtsOrderServiceImpl;
    @Autowired
    private DtsOrderGoodsService dtsOrderGoodsServiceImpl;
    @Autowired
    private DtsUserService dtsUserServiceImpl;
    /**
     * http://localhost:8080/admin/order/list?page=1&limit=20&orderStatusArray=103&sort=add_time&order=desc&userId=1&orderSn=2
     * get
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:order:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "商场管理"}, button = "查询订单")
    public Object list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer limit, Integer[] orderStatusArray, String sort, String order, Integer userId, String orderSn){
        if (page<1){
            page=1;
        }
        if (limit<1){
            limit=20;
        }
       IPage<DtsOrder> IPage= dtsOrderServiceImpl.findDtsOrderByPage(page,limit,orderStatusArray,sort,order,userId,orderSn);
        Map data=new HashMap<>();
        data.put("total",IPage.getTotal());
        data.put("items",IPage.getRecords());
        return ResponseUtil.ok(data);
    }
    /**
     * http://localhost:8080/admin/order/listShipChannel
     * get
     */
    @GetMapping("/listShipChannel")
    @RequiresPermissions("admin:order:listShipChannel")
    @RequiresPermissionsDesc(menu = {"系统管理", "商场管理"}, button = "查询快递列表")
    public Object listShipChannel(){
        List<ShipChannelVo> shipChannelVoList=new ArrayList<>();
        shipChannelVoList.add(new ShipChannelVo("HTKY","百世快递"));
        shipChannelVoList.add(new ShipChannelVo("ZTO","中通快递"));
        shipChannelVoList.add(new ShipChannelVo("YTO","圆通速递"));
        shipChannelVoList.add(new ShipChannelVo("YD","韵达速递"));

        Map map=new HashMap();
        map.put("shipChannelList",shipChannelVoList);
        return ResponseUtil.ok(map);
    }
    /**
     * http://localhost:8080/admin/order/detail?id=13
     * get
     */
    @GetMapping("/detail")
    @RequiresPermissions("admin:order:detail")
    @RequiresPermissionsDesc(menu = {"系统管理", "商场管理"}, button = "查询订单详情")
    public Object detail(@RequestParam("id") Integer oid){
        //检验参数
        if (oid==null){
            return ResponseUtil.badArgument();
        }
        //查询订单
        DtsOrder dtsOrder=dtsOrderServiceImpl.findDtsOrderById(oid);
        //查询订单详情
       List<DtsOrderGoods> orderGoods=dtsOrderGoodsServiceImpl.findDtsOrderGoodsByOid(oid);
        //查询用户数据
        DtsUser dtsUser=dtsUserServiceImpl.findDtsUserById(dtsOrder.getUserId());
        UserVo userVo=new UserVo();
        userVo.setNickname(dtsUser.getNickname());
        userVo.setAvatar(dtsUser.getAvatar());
        //查询订单数据
        Map map=new HashMap();
        map.put("orderGoods",orderGoods);
        map.put("user",userVo);
        map.put("order",dtsOrder);
        return ResponseUtil.ok(map);
    }

}
